package de.stefanocke.roo.wicket.form.formfields;

import static de.stefanocke.roo.wicket.Stereotypes.COMPLEX_FORMFIELD;
import static de.stefanocke.roo.wicket.Stereotypes.COMPLEX_LIST_EDITOR_FIELD;
import static de.stefanocke.roo.wicket.Stereotypes.FORM_FIELDS;
import static de.stefanocke.roo.wicket.Stereotypes.SIMPLE_LIST_EDITOR_FIELD;

import java.util.List;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem;

import de.stefanocke.roo.wicket.RooFormFields;
import de.stefanocke.roo.wicket.base.BeanPropertiesAwareMetadataProvider;
import de.stefanocke.roo.wicket.base.ModelClassAwareMetadataCreationParams;
import de.stefanocke.roo.wicket.form.FormComponentMetadata;
import de.stefanocke.roo.wicket.model.BeanPropertyMetadata;
import de.stefanocke.roo.wicket.model.BeanPropertyMetadataWithDetails;

/**
 * Provides {@link FormFieldsMetadata}.
 * 
 */
@Component
@Service
public final class FormFieldsMetadataProvider extends
		BeanPropertiesAwareMetadataProvider<FormFieldsMetadata, FormFieldsAnnotationValues> {

	private static final String[] STEREOTYPES = new String[] { FORM_FIELDS, COMPLEX_FORMFIELD,
			SIMPLE_LIST_EDITOR_FIELD, COMPLEX_LIST_EDITOR_FIELD };

	public FormFieldsMetadataProvider() {
		super(RooFormFields.class, FormFieldsMetadata.class, FormFieldsAnnotationValues.class,
				FormComponentMetadata.class);
	}

	@Override
	protected Class<FormFieldsHtmlBuilder> getHtmlBuilderClass() {
		return FormFieldsHtmlBuilder.class;
	}

	@Override
	protected ItdTypeDetailsProvidingMetadataItem getMetadataInternal(
			ModelClassAwareMetadataCreationParams<FormFieldsAnnotationValues> cmcp) {
		List<BeanPropertyMetadata> propertyMetadata = getBeanPropertiesMetadata(cmcp);
		List<BeanPropertyMetadataWithDetails<FormComponentMetadata>> ffPropertyMetadata = createBeanPropertyMetadataWithDetails(
				cmcp.getMid(), propertyMetadata, FormComponentMetadata.class);

		FormFieldsMetadata formFieldsMetadata = new FormFieldsMetadata(cmcp, ffPropertyMetadata);
		return formFieldsMetadata;
	}

	@Override
	protected String[] requiredStereotypes() {
		return STEREOTYPES;
	}
}